package com.globallogic.javatrainings.sergiiivanov.tests;

import java.util.ArrayList;
import java.util.LinkedList;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.MyArray;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityFailure;

/**
 * Implements performance testing for {@link MyArray}<br>
 * in comparison with {@link ArrayList} and {@link LinkedList}<br>
 * <b>First run results:</b><br>
 * <li>{@link #compareMyArrayDelete()} - 0.165 <li>{@link #compareMyArrayFill()}
 * - 0.135 <li>{@link #compareMyArrayRead()} - 0.130
 * <hr>
 * <li>{@link #compareLinkedListDelete()} - 0.010
 * <li>{@link #compareLinkedListFill()} - 0.002
 * <li>{@link #compareLinkedListRead()} - 0.181
 * <hr>
 * <li>{@link #compareArrayListDelete()} - 0.031
 * <li>{@link #compareArrayListFill()} - 0.003
 * <li>{@link #compareArrayListRead()} - 0.002
 * <hr>
 * <b>Run results after optimization:</b><br>
 * not implemented yet
 * 
 * @author Sergey
 * 
 */
public class MyArrayTestPerformance {

    final int arraySize = 10000;

    @Test
    public void compareMyArrayFill() {
        MyArray<Integer> array = new MyArray<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
    }

    @SuppressWarnings("unused")
    @Test
    public void compareMyArrayRead() throws UniversityFailure {
        MyArray<Integer> array = new MyArray<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        int t1, t2;
        for (int i = 0; i < arraySize; i++) {
            t1 = array.get(i);
            t2 = array.get(arraySize - i - 1);
        }
    }

    @Test
    public void compareMyArrayDelete() throws UniversityFailure {
        MyArray<Integer> array = new MyArray<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        for (int i = 0; i < this.arraySize * .9; i++) {
            if (i % 10 == 0) {
                array.remove(i);
            }
        }
    }

    @Test
    public void compareArrayListFill() {
        ArrayList<Integer> array = new ArrayList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
    }

    @SuppressWarnings("unused")
    @Test
    public void compareArrayListRead() {
        ArrayList<Integer> array = new ArrayList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        int t1, t2;
        for (int i = 0; i < arraySize; i++) {
            t1 = array.get(i);
            t2 = array.get(arraySize - i - 1);
        }
    }

    @Test
    public void compareArrayListDelete() {
        ArrayList<Integer> array = new ArrayList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        for (int i = 0; i < this.arraySize * .9; i++) {
            if (i % 10 == 0) {
                array.remove(i);
                array.trimToSize();
            }
        }
    }

    @Test
    public void compareLinkedListFill() {
        LinkedList<Integer> array = new LinkedList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
    }

    @SuppressWarnings("unused")
    @Test
    public void compareLinkedListRead() {
        LinkedList<Integer> array = new LinkedList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        int t1, t2;
        for (int i = 0; i < arraySize; i++) {
            t1 = array.get(i);
            t2 = array.get(arraySize - i - 1);
        }
    }

    @Test
    public void compareLinkedListDelete() {
        LinkedList<Integer> array = new LinkedList<Integer>();
        for (int i = 0; i < arraySize; i++)
            array.add(i);
        for (int i = 0; i < this.arraySize * .9; i++) {
            if (i % 10 == 0) {
                array.remove(i);
            }
        }
    }

}
